package 将有序数组转换为二叉搜索树;

import java.util.Arrays;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 将有序数组转换为二叉搜索树
 * @date 2022/11/12 15:42
 */
public class Demo108 {


    public   TreeNode sortedArrayToBST(int[] nums) {
        if(nums.length==0){
            return null;
        }
        TreeNode root=new TreeNode(nums[nums.length/2]);
        int[] left = Arrays.copyOfRange(nums, 0, Math.max(0, nums.length / 2));
        root.left=sortedArrayToBST(left);
        int[] right = Arrays.copyOfRange(nums, nums.length / 2 + 1,nums.length );
        root.right=sortedArrayToBST(right);
        return root;
    }



    //Definition for a binary tree node.
    public  class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
